package cn.tedu.ivos.dictoption.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.dictoption.pojo.dto.DictOptionParam;
import cn.tedu.ivos.dictoption.pojo.dto.DictOptionQuery;
import cn.tedu.ivos.dictoption.pojo.vo.DictOptionVO;
import cn.tedu.ivos.dictoption.service.DictOptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "字典项模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/v1/dictOption/")
public class DictOptionController {
    @Autowired
    DictOptionService dictOptionService;

    @ApiOperation("查询字典项")
    @GetMapping("select")
    public JsonResult selectAll(@Validated DictOptionQuery dictOptionQuery) {
        log.debug("开始处理【查询字典项】的请求，参数：{}", dictOptionQuery);
        List<DictOptionVO> list = dictOptionService.selectAll(dictOptionQuery);
        return JsonResult.ok(list);
    }

    @ApiOperation("保存字典项")
    @PostMapping("save")
    public JsonResult saveDictOption(@Validated DictOptionParam dictOptionParam) {
        log.debug("开始处理【保存字典项】的请求，参数：{}", dictOptionParam);
        dictOptionService.saveDictOption(dictOptionParam);
        return JsonResult.ok();
    }

    @ApiOperation("删除字典项")
    @PostMapping("delete/{id}")
    public JsonResult deleteDictOption(@PathVariable Long id) {
        log.debug("开始处理【删除字典项】的请求，参数：{}", id);
        dictOptionService.deleteDictOption(id);
        return JsonResult.ok();
    }

    /*
    *  定义一个根据字典code查询该字典下所有字典项的方法
    * */
    @ApiOperation("根据字典code查询该字典下所有字典项")
    @GetMapping("select/{code}")
    public JsonResult selectByCode(@PathVariable String code) {
        log.debug("开始处理【根据字典code查询该字典下所有字典项】的请求，参数：{}", code);
        List<DictOptionVO> list = dictOptionService.selectDictOptionByCode(code);
        return JsonResult.ok(list);
    }
}
